<?php

namespace App\Services\Auth;

use Hash;
use App\Entities\Auth\Manager;
use App\Repositories\Auth\ManagerRepository;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class ManagerService
{
    protected $repository;

    public function __construct(ManagerRepository $repository)
    {
        $this->repository = $repository;
    }

    public function getPageList()
    {
        try {
            $request = request();
            $res = $this->repository->paginate($request['limit'])->toArray();

            $data = [
                'code' => 0,
                'msg' => '正在请求中...',
                'count' => $res['total'],
                'data' => $res['data'],
            ];

            return $data;
        } catch (Exception $ex) {
            Log::error('Manager getPageList Error', [$ex->getMessage()]);

            return null;
        }
    }

    public function create($data)
    {
        try {
            $manager = new Manager($data);
            if ($manager->useCreated()) {
                $manager->setCreated();
            }
            $manager = $manager->toArray();
            $manager['password'] = $data['password'];
            $this->repository->create($manager);

            return true;
        } catch (Exception $ex) {
            Log::error('Manager Create Error', [$ex->getMessage()]);

            return false;
        }
    }

    public function changePassword($id, $data, $type)
    {
        try {
            switch ($type) {
                case 'auth':

                    break;
                case 'change':
                    break;
                default:
                    $this->repository->update(['password' => $data['new_pass']], $id);
                    break;
            }
            // if ('change' == $type || 'auth' == $type) {
            //     $result = $this->checkPassword($id, $data['old_pass']);

            //     if (!$result['flag']) {
            //         return $result;
            //     }
            // }
            // $this->repository->update(['password' => $data['new_pass']], $id);

            // return ['flag' => true, 'message' => '原密码正确'];
        } catch (Exception $ex) {
            Log::error('Manager Change Password id="'.$id.'" Error', [$ex->getMessage()]);

            return ['flag' => false, 'message' => $ex->getMessage()];
        }
    }

    public function checkPassword($id, $data)
    {
        try {
            $manager = $this->repository->find($id);
            if ($manager) {
                if (Hash::check($data, $manager->password)) {
                    return ['flag' => true, 'message' => '原密码正确'];
                } else {
                    return ['flag' => false, 'message' => '原密码错误'];
                }
            } else {
                return ['flag' => false, 'message' => '用户不存在'];
            }
        } catch (Exception $ex) {
            Log::error('Manager checkPassword id="'.$id.'" Error', [$ex->getMessage()]);
        }
    }

    public function update($id, $data)
    {
        try {
            $manager = new Manager($data);
            $this->repository->SaveData($manager, $id);

            return true;
        } catch (Exception $ex) {
            Log::error('Manager Update id="'.$id.'" Error', [$ex->getMessage()]);

            return false;
        }
    }

    public function show($id)
    {
        try {
            return $this->repository->find($id);
        } catch (Exception $ex) {
            Log::error('Manager Load id="'.$id.'" Error', [$ex->getMessage()]);

            return null;
        }
    }

    public function remove($ids)
    {
        DB::beginTransaction();
        try {
            $manager = $this->repository->find($ids);
            //删除用户角色关系
            $manager->role()->detach();
            //删除用户
            $manager->delete();

            DB::commit();

            return true;
        } catch (Exception $ex) {
            DB::rollBack();
            Log::error('Manager Delete'.$ids.' Error', [$ex->getMessage()]);

            return false;
        }
    }

    public function saveRole($id, $data)
    {
        DB::beginTransaction();
        try {
            $manager = $this->repository->find($id);
            //移除角色
            $manager->role()->detach();
            //添加角色
            $manager->role()->attach($data, ['is_default' => 0]);
            DB::commit();

            return true;
        } catch (Exception $ex) {
            DB::rollBack();
            Log::error('Manager SaveRole'.$id.' Error', [$ex->getMessage()]);

            return false;
        }
    }
}
